import sun.applet.AppletAudioClip;

import javax.swing.*;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @classname 绘制城镇背景和npc
 * @Author 姬如千泷
 * @Date 2021/5/15  0:17
 * @Version 1.0
 */
public class MyTown extends JPanel {
    MyCharacter ct_label;//当前角色
    MyFrame myFrame;//当前主屏幕
    public int bg_x = 0;
    Timer npcTurn;//npc动作的定时器
    Timer protect;//一个特殊的守护线程
    CharacterVoice voice = new CharacterVoice();//定时播放角色声音
    AudioClip npc2_voice = new AppletAudioClip(DateCenter.npc2_voiceUrl);
    AudioClip npc2_voice1 = new AppletAudioClip(DateCenter.npc2_voice1Url);
    AudioClip npc1_voice = new AppletAudioClip(DateCenter.npc1_voiceUrl);
    AudioClip npc1_voice1 = new AppletAudioClip(DateCenter.npc1_voice1Url);
    public MyTown(MyFrame myFrame,int x) {
        this.myFrame = myFrame;
        bg_x=x;
        init();
    }
    public void init() {
        this.setLayout(null);
        JLabel label = new JLabel(DateCenter.background);
        label.setBounds(0, 0, 2200, 800);
        this.setBounds(bg_x, 0, 2200, 900);
        this.add(label);
        Npc npc1 = new Npc(600, 400,DateCenter.npc1_stand);
        Npc npc2 = new Npc(1200, 400,DateCenter.npc2_stand);
        this.add(npc1, 0);
        this.add(npc2, 0);
        protection(npc1,npc2);
        myFrame.add(this, 0);
    }

    public void move() {
        this.setLocation(bg_x, 0);
    }

    //定时播放人物声音的类
    class CharacterVoice implements ActionListener{
        public Timer timer = new Timer(6000,this);//每6秒播放一次
        AudioClip stand_voice1 = new AppletAudioClip(DateCenter.stand_voice1Url);
        AudioClip stand_voice2 = new AppletAudioClip(DateCenter.stand_voice2Url);
        AudioClip stand_voice3 = new AppletAudioClip(DateCenter.stand_voice3Url);
        AudioClip stand_voice4 = new AppletAudioClip(DateCenter.stand_voice4Url);
        AudioClip stand_voice5 = new AppletAudioClip(DateCenter.stand_voice5Url);
        @Override
        public void actionPerformed(ActionEvent e) {
            if(!ct_label.juage_attack&&!ct_label.juage_jump&&!ct_label.juage_skill){
                int i = (int)(Math.random()*5);
                switch (i){
                    case 0:{stand_voice1.play();break;}
                    case 1:{stand_voice2.play();break;}
                    case 2:{stand_voice3.play();break;}
                    case 3:{stand_voice4.play();break;}
                    case 4:{stand_voice5.play();break;}
                }

            }
        }
        public CharacterVoice(){
            timer.start();
        }
    }
    public void protection(Npc npc1 , Npc npc2) {
        npcTurn = new Timer(10000, (e) -> {//每10s npc转一次圈
            new Thread(() -> {
                for (int i = 0; i < 4; i++) {
                    npc2.setIcon(juage(i, 0));
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                }
                npc2.setIcon(DateCenter.npc2_stand);
                for (int i = 0; i < 8; i++) {
                    npc1.setIcon(juage(i, 1));
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                }
                npc1.setIcon(DateCenter.npc1_stand);
            }).start();
        });
        npcTurn.start();
        protect = new Timer(100, (e) -> {
            forProtect(npc1,npc2);
        });
        protect.start();
    }
    public void forProtect(Npc npc1,Npc npc2){
        //每0.1s检测一次角色是否在npc1范围内
        if (Math.abs(ct_label.ct_x - npc1.npc_x) < 100 && Math.abs(ct_label.ct_y - npc1.npc_y) < 100) {
            if (!npc1.isIn) {
                npc1.flag++;
                if (npc1.flag % 2 == 0)
                    npc1_voice.play();
                else npc1_voice1.play();
            }
            npc1.isIn = true;
        } else npc1.isIn = false;
        //每0.1s检测一次角色是否在npc2范围内
        if (Math.abs(ct_label.ct_x - npc2.npc_x) < 100 && Math.abs(ct_label.ct_y - npc2.npc_y) < 100) {
            if (!npc2.isIn) {
                npc2.flag++;
                if (npc2.flag % 2 == 0)
                    npc2_voice.play();
                else npc2_voice1.play();
            }
            npc2.isIn = true;
        } else npc2.isIn = false;
        //判断是否需要加载地图
        if (ct_label.ct_x > 2200) {
            myFrame.dispose();
            new MyChoose();
            npcTurn.stop();
            voice.timer.stop();
            protect.stop();
        }
    }
    public ImageIcon juage(int i, int k) {
        if (k == 0) {
            switch (i) {
                case 0:
                    return DateCenter.npc2_turn1;
                case 1:
                    return DateCenter.npc2_turn2;
                case 2:
                    return DateCenter.npc2_turn3;
                case 3:
                    return DateCenter.npc2_turn4;
                default:
                    return null;
            }
        } else {
            switch (i) {
                case 0:
                    return DateCenter.npc1_action1;
                case 1:
                    return DateCenter.npc1_action2;
                case 2:
                    return DateCenter.npc1_action3;
                case 3:
                    return DateCenter.npc1_action4;
                case 4:
                    return DateCenter.npc1_action5;
                case 5:
                    return DateCenter.npc1_action6;
                case 6:
                    return DateCenter.npc1_action7;
                case 7:
                    return DateCenter.npc1_action8;
                default:
                    return null;
            }

        }
    }
    class Npc extends JLabel {
        public int npc_x, npc_y;//npc的坐标
        public boolean isIn = false;//一个判断角色是否在npc范围内的标志位
        public int flag=0;//计数器
        public Npc(int x,int y,ImageIcon c) {
            npc_x =x;
            npc_y =y;
            this.setBounds(npc_x, npc_y, 200, 200);
            this.setIcon(c);
        }
    }
}
